package com.rljk.controller.app;/**
 * Created by mac on 2018/8/15.
 */

import com.rljk.common.controller.BaseController;
import com.rljk.common.result.CodeMsg;
import com.rljk.common.result.Result;
import com.rljk.domain.common.PageData;
import com.rljk.redis.RedisService;
import com.rljk.service.NoticeInfoManager;
import com.rljk.service.NoticeManager;
import com.rljk.utils.AppUtil;
import com.rljk.utils.RelativeDateFormat;
import com.rljk.utils.Tools;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName NoticeInController
 * @Description
 * @Author zhang
 * @Date 2018/8/15 12:00
 */
@RestController
@Slf4j
@RequestMapping("/notice")
public class NoticeInController extends BaseController {

    @Resource(name = "noticeService")
    private NoticeManager noticeService;

    @Resource(name = "noticeInfoService")
    private NoticeInfoManager noticeInfoService;

    @Autowired
    private RedisService redisService;


    /**
     * @Author zhang
     * @Description  通过类型获取公告等列表接口
     * @Date 2018/8/15 13:22
     * @Param []
     * @return java.lang.Object
     **/
    @RequestMapping(value="/noticelist")
    public Object getCaidanNotice() {
        log.info("通过类型获取公告等列表接口");
        PageData pd = new PageData();
        pd = this.getPageData();
        log.info("请求参数："+pd.toString());
        Map<String,Object> map = new HashMap<>();
        try {
            if (Tools.checkKey("GETCAIDANNOTICE", pd.getString("FKEY"))) {
                if (AppUtil.checkParam("getCaidanNotice",pd)){
                    String userId = redisService.get((String) pd.get("TOKEN"));
                    String noticeType = (String) pd.get("NOTICE_TYPE");
                    if (StringUtils.equals(noticeType,"2")){
                        pd.put("USER_ID",userId);
                    }
                    List<PageData> notice = noticeInfoService.listAll(pd);
                    if (notice.size() != 0){
                        return Result.success(notice);
                    }else {
                        return Result.success(notice);
                    }
                }else {
                    return Result.error(CodeMsg.PARAM_LACK);
                }
            } else {
                return Result.error(CodeMsg.FKEY_FAIL);
            }
        } catch (Exception e) {
            log.error(e.toString(), e);
            return Result.error(CodeMsg.SERVER_ERROR);
        }
    }


    /**
     * @Author zhang
     * @Description  获取通知列表
     * @Date 2018/8/15 14:00
     * @Param []
     * @return java.lang.Object
     **/
    @RequestMapping(value="/noticeinfo")
    public Object getNoticeInfo() {
        log.info("获取通知列表");
        PageData pd = new PageData();
        pd = this.getPageData();
        log.info("请求参数："+pd.toString());
        Map<String,Object> map = new HashMap<>();
        try {
            if (Tools.checkKey("GETNOTICEINFO", pd.getString("FKEY"))) {
                if (AppUtil.checkParam("getNoticeInfo",pd)){

                    String userId = redisService.get((String) pd.get("TOKEN"));
                    List<PageData> notice = noticeService.listAll(pd);
                    if (notice.size() != 0){

                        for (PageData pageData : notice){

                            Integer noticeType = (Integer) pageData.get("NOTICE_TYPE");
                            pd.put("NOTICE_TYPE",noticeType);
                            if (noticeType == 2){
                                pd.put("USER_ID",userId);
                            }
                            log.info(pd.toString());
                            PageData nowNoticeInfo = noticeInfoService.nowNoticeInfo(pd);
                            if (nowNoticeInfo != null){
                                pageData.put("NOTICEINFO_TITLE",nowNoticeInfo.get("NOTICEINFO_TITLE"));
                                pageData.put("TIME", RelativeDateFormat.format((String) nowNoticeInfo.get("CREAT_TIME")));
                            }else {
                                pageData.put("NOTICEINFO_TITLE","暂无最新消息");
                                pageData.put("TIME", "");
                            }
                        }
                        return Result.success(notice);
                    }else {
                        return Result.success(notice);
                    }

                }else {
                    return Result.error(CodeMsg.PARAM_LACK);
                }
            } else {
                return Result.error(CodeMsg.FKEY_FAIL);
            }
        } catch (Exception e) {
            log.error(e.toString(), e);
            return Result.error(CodeMsg.SERVER_ERROR);
        }
    }


}
